// pages/current/current.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    taskList:[],
    rescuePos:'',
    taskId:''
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.cloud.callFunction({
      name: 'getOpenid',
      complete: res => {
        console.log('云函数获取到的openid: ', res.result.openId)
        var openid = res.result.openId;
        this.setData({
          openid: openid
        })
        wx.request({
          url: 'https://www.lanotherl.com/volunteer/getActiveTaskList',
          data:{
            openid:openid
          },
          complete:res => {
            this.setData({
              taskList:res.data.data.taskList
            })
            console.log("任务列表为：",this.data.taskList)
          }
        })
      }
    })
  },
  /**
   * 选择当前地点
  */
 chooseNowAddress: function () {
  var _page = this;
  wx.chooseLocation({ //选择当前地点，调用chooseLocation
    success: res => {
      _page.setData({
        rescuePos: res.name,
      });
      wx.request({
        url: 'https://www.lanotherl.com/task/end',
        method:"PUT",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data:{
          rescuePos:this.data.rescuePos,
          taskId:this.data.taskId
        },
        success:res=>{
          console.log("结束任务：",res)
          console.log("rescuePos:",this.data.rescuePos)
          console.log("taskId:",this.data.taskId)
        },
        fail:err=>{
          console.log("发生错误：",err)
        }
      })
    },
    fail: e => {
      console.log(e);
      // 判断用户是否拒绝了授权
      wx.getSetting({
        success: res => {
          if (typeof(res.authSetting['scope.userLocation']) != 'undefined' && !res.authSetting['scope.userLocation']) {
            // 用户拒绝了授权
            wx.showModal({
              title: '提示',
              content: '您拒绝了定位权限，将无法使用该功能',
              success: res => {
                if (res.confirm) {
                  // 跳转设置页面
                  wx.openSetting({
                    success: res => {
                      if (res.authSetting['scope.userLocation']) {
                        // 授权成功，重新定位
                        wx.getLocation({
                          success: res => {}
                        });
                      } else {
                        // 没有允许定位权限
                        wx.showToast({
                          title: '您拒绝了定位权限，将无法使用该功能',
                          icon: 'none'
                        })
                      }
                    }
                  });
                }
              }
            });
          }
        }
      });
    }
  });
  },
  /**
   * 点击结束事件
   */
  endTask:function(e){
    this.setData({
      taskId:e.target.id
    })
    console.log("设置taskId为：",this.data.taskId);
    this.chooseNowAddress();
  },

})